<?
class InputCaptcha extends Input
{
	var $nLength = 4;
	var $nWidth ;
	var $nHeight ;
	var $cFont ;
	
	function InputCaptcha( $cLabel, $cField, $nLength = 4, $nWidth = 24, $nHeight = 24)
	{
		$this->cLabel = $cLabel;
		$this->cField = $cField;
		$this->nLength = $nLength;
		$this->nHeight = $nHeight;
		$this->nWidth = $nWidth;
		$this->cFont =  PATH_MODULES . '/base/componentes/georgiab.ttf';
	}

	function reload()
	{
		echo 	$this->makeImage();
		exit();
	}
	function makeImage()
	{
		$cCode  = $this->generateCode();
		$_SESSION['captcha'] = $cCode;

		$cReturn = "";
		for( $nIndex = 0 ; $nIndex < $this->nLength ; $nIndex++ )
		{
			$cReturn .= "<img style='border: red 1px solid;margin-left:1px;vertical-align: bottom;' src='?" . PARAM_REFRESH . "=captcha&amp;id=" . time(). "&amp;nPosicion=$nIndex&amp;" . PARAM_METHOD ."=getImage&amp;" . PARAM_FIELD . "=" . $this->cField . "' />";
			//echo "?" . PARAM_REFRESH . "=captcha&amp;nPosicion=$nIndex&amp;" . PARAM_METHOD ."=getImage&amp;" . PARAM_FIELD . "=" . $this->cField ;
		}
		return $cReturn ;
	}
	function make()
	{
		$cReturn = "<div>";
		$cPropiedades = $this->getProperties();
		$cEventos     = $this->getEvents();
		if ( $this->nLength ) $cPropiedades .= " maxlength='" . $this->nLength . "' ";
		$cReturn .= "<input onblur='return checkText(this, \"Codigo\" , true );' type='text'  size='4' $cPropiedades $cEventos id='" . $this->cField. "' name='" . $this->cField . "' value='' />";

		$cReturn .= "<div id='code_" . $this->cField  . "' style='display:inline;' >";
		$cReturn .= $this->makeImage();
		$cReturn .= "&nbsp;</div>";

		$cReturn .= "&nbsp;<a  href='javascript: void(0)' onclick='send(\"?" . PARAM_REFRESH . "=captcha&amp;" . PARAM_METHOD ."=reload&amp;" . PARAM_FIELD . "=" . $this->cField . "\", refreshElement, \"code_" . $this->cField  . "\");' >recargar</a>";
		$cReturn .= "</div>";
		return $cReturn ;
	}
	function getImage()
	{
		$nPosicion = getParam('nPosicion');
		$cCharacter = $_SESSION['captcha'][$nPosicion];
		/* font size will be 75% of the image height */
		$font_size = $this->nHeight * 0.5;
		$image = @imagecreate($this->nWidth, $this->nHeight) or die('Cannot Initialize new GD image stream');
		/* set the colours */
		$background_color = imagecolorallocate($image, 245, 240, 240);
		$text_color = imagecolorallocate($image, 25, 24, 20);
		//$noise_color = imagecolorallocate($image, 100, 120, 180);

			/* generate random dots in background
			for( $i=0; $i<($this->nWidth*$this->nHeight)/3; $i++ ) {
				imagefilledellipse($image, mt_rand(0,$this->nWidth), mt_rand(0,$this->nHeight), 1, 1, $noise_color);
			}
			*/
			/* generate random lines in background
			for( $i=0; $i<($this->nWidth*$this->nHeight)/150; $i++ ) {
				imageline($image, mt_rand(0,$this->nWidth), mt_rand(0,$this->nHeight), mt_rand(0,$this->nWidth), mt_rand(0,$this->nHeight), $noise_color);
			}
			*/
			/* create textbox and add text */
			$textbox = imagettfbbox($font_size, 0, $this->cFont, $cCharacter);
			$x = ($this->nWidth - $textbox[4])/2;
			$y = ($this->nHeight - $textbox[5])/2;
			$angulo = rand(-30, 30);
			imagettftext($image, $font_size, $angulo, $x, $y, $text_color, $this->cFont , $cCharacter);
			//imagerotate( resource $source_image  , float $angle  , int $bgd_color  [, int $ignore_transparent  ] )

			/* output captcha image to browser */
			header('Content-Type: image/jpeg');
			imagejpeg($image);
			imagedestroy($image);
			exit();
		}

		function generateCode()
		{
			$cCharacters = '23456789bcdfghjkmnpqrstvwxyz';
			$cCode = '';
			$i = 0;
			while ($i < $this->nLength ) {
				$cCode .= substr($cCharacters, mt_rand(0, strlen($cCharacters)-1), 1);
				$i++;
			}
			return $cCode;
		}
	}
?>